home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 2.iso / STUTTGART / LANG / GNUST / !GNUst / st / Interval < prev    next >
Text File  |  1991-09-13  |  5KB  |  213 lines

  1. "======================================================================
  2. |
  3. |   Interval Method Definitions
  4. |
  5.  ======================================================================"
  6.  
  7.  
  8. "======================================================================
  9. |
  10. | Copyright (C) 1990, 1991 Free Software Foundation, Inc.
  11. | Written by Steve Byrne.
  12. |
  13. | This file is part of GNU Smalltalk.
  14. |
  15. | GNU Smalltalk is free software; you can redistribute it and/or modify it
  16. | under the terms of the GNU General Public License as published by the Free
  17. | Software Foundation; either version 1, or (at your option) any later version.
  18. | GNU Smalltalk is distributed in the hope that it will be useful, but WITHOUT
  19. | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  20. | FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
  21. | details.
  22. | You should have received a copy of the GNU General Public License along with
  23. | GNU Smalltalk; see the file COPYING.  If not, write to the Free Software
  24. | Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  
  25. |
  26.  ======================================================================"
  27.  
  28.  
  29. "
  30. |     Change Log
  31. | ============================================================================
  32. | Author       Date       Change 
  33. | sbyrne     25 Apr 89      created.
  34. |
  35. "
  36.  
  37. SequenceableCollection subclass: #Interval
  38.                instanceVariableNames: 'start stop step'
  39.                classVariableNames: ''
  40.                poolDictionaries: ''
  41.                category: nil.
  42.  
  43. Interval comment: 
  44. 'My instances represent ranges of objects, typically Magnitude type
  45. objects.  I provide iteration/enumeration messages for producing all the
  46. members that my instance represents.' !
  47.  
  48. !Interval class methodsFor: 'instance creation'!
  49.  
  50. from: startInteger to: stopInteger by: stepInteger
  51.     | int |
  52.     ^self new initializeFrom: startInteger to: stopInteger by: stepInteger
  53. !
  54.  
  55. from: startInteger to: stopInteger    
  56.     ^self from: startInteger to: stopInteger by: 1
  57.  
  58. !!
  59.  
  60.  
  61.  
  62. !Interval methodsFor: 'basic'!
  63.  
  64. " Note to the reader: these two methods (do: and collect:) are implemented
  65. in this expanded way, instead of just making the whileTrue: condition be a
  66. block that's conditionally assigned based on the sign of step and then
  67. invoking a single whileTrue: loop.  However, in this form, the optimizer
  68. can optimize these while loops into direct byte codes and not have to bother
  69. with expensive (relatively) block and method context creation and sending
  70. messages to blocks. "
  71.  
  72. do: aBlock
  73.     | i |
  74.     i _ start.
  75.     step > 0
  76.         ifTrue: [
  77.             [ i <= stop ] whileTrue:
  78.                 [ aBlock value: i.
  79.                   i _ i + step ]
  80.         ]
  81.         ifFalse: [
  82.         [ i >= stop ] whileTrue:
  83.                 [ aBlock value: i.
  84.                   i _ i + step ]
  85.         ]
  86. !
  87.  
  88. collect: aBlock
  89.     | i result j |
  90.     result _ self species new: self size.
  91.     i _ 1.
  92.     j _ start.
  93.     step > 0
  94.         ifTrue: [
  95.         [ j <= stop ]
  96.             whileTrue:
  97.             [ result at: i put: (aBlock value: j).
  98.                 j _ j + step.
  99.             i _ i + 1 ]
  100.         ]
  101.     ifFalse: [
  102.         [ j >= stop ]
  103.             whileTrue:
  104.             [ result at: i put: (aBlock value: j).
  105.               j _ j + step.
  106.               i _ i + 1 ]
  107.         ]
  108. !    
  109.  
  110. size
  111.     step > 0
  112.         ifTrue: [
  113.         stop >= start ifTrue: [ ^(stop - start) // step + 1 ]
  114.                       ifFalse: [ ^0 ]
  115.     ]
  116.     ifFalse: [
  117.             start >= stop ifTrue: [ ^(start - stop) // step + 1 ]
  118.                       ifFalse: [ ^0 ]
  119.     ]
  120. !
  121.  
  122. species
  123.     ^Array
  124. !
  125.  
  126.  
  127. at: index
  128.     (index >= 1 and: [index <= self size])
  129.         ifTrue: [ ^start + (step * (index - 1)) ]
  130.     ifFalse: [ self error: 'subscript out of bounds' ]
  131. !
  132.  
  133. at: index put: anObject
  134.     self error: 'you cannot store into an Interval'
  135. !
  136.  
  137. add: newObject
  138.     self error: 'elements cannot be add3ed to an Interval'
  139. !
  140.  
  141. remove: newObject
  142.     self error: 'elements canot be removed from an Interval'
  143. !!
  144.  
  145.  
  146.  
  147. !Interval methodsFor: 'testing'!
  148.  
  149. = anInterval
  150.     ^(start = anInterval start) &
  151.     (stop = anInterval stop) &
  152.     (step = anInterval step)
  153. !
  154.  
  155. hash
  156.     ^(start bitXor: stop) bitXor: step
  157. !!
  158.  
  159.  
  160.  
  161. !Interval methodsFor: 'printing'!
  162.  
  163. printOn: aStream
  164.     aStream nextPutAll: self classNameString.
  165.     aStream nextPut: Character space.
  166.     start printOn: aStream.
  167.     aStream nextPut: $,.
  168.     stop printOn: aStream.
  169.     aStream nextPut: $,.
  170.     step printOn: aStream
  171. !!
  172.  
  173.  
  174.  
  175. !Interval methodsFor: 'storing'!
  176.  
  177. storeOn: aStream
  178.     aStream nextPut: $(.
  179.     aStream nextPutAll: self classNameString.
  180.     aStream nextPutAll: ' from: '.
  181.     start storeOn: aStream.
  182.     aStream nextPutAll: ' to: '.
  183.     stop storeOn: aStream.
  184.     aStream nextPutAll: ' by: '.
  185.     step storeOn: aStream.
  186.     aStream nextPut: $)
  187. !!
  188.  
  189.  
  190.  
  191. !Interval methodsFor: 'private methods'!
  192.  
  193. initializeFrom: startInteger to: stopInteger by: stepInteger
  194.     start _ startInteger.
  195.     stop _ stopInteger.
  196.     step _ stepInteger
  197. !
  198.  
  199. start
  200.     ^start
  201. !
  202.  
  203. stop
  204.     ^stop
  205. !
  206.  
  207. step
  208.     ^step
  209. !!
  210.  
  211.